//
//  RBMediaSlider.m
//  RBVideoPlayer
//
//  Created by Lan on 2017/8/7.
//  Copyright © 2017年 SummerTea. All rights reserved.
//

#import "RBMediaSlider.h"
#import "UIView+RBMediaAdd.h"

@interface RBMediaSlider ()

@property(nonatomic, strong) UIView *dragView;
@property(nonatomic, strong) UIImageView *dragImgView;
@property(nonatomic, strong) UIView *railView;
@property(nonatomic, strong) UIView *playTimeView;
@property(nonatomic, strong) UIView *cacheView;
@property(nonatomic, assign) CGPoint beganPoint;

@end

@implementation RBMediaSlider

#pragma mark - System

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.dragViewHeight = 14;
        self.railView.hidden = NO;
        self.cacheView.hidden = NO;
        self.playTimeView.hidden = NO;
        self.dragView.hidden = NO;
        
        self.cacheProgress = 0;
        self.playProgress = 0;
        
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRailEvent:)]];
        [self setResponseArea:UIEdgeInsetsMake(0, 0, 14, 0)];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.railView.frame = ({
        CGFloat w = self.frame.size.width - self.dragViewHeight;
        CGFloat h = 3;
        CGFloat x = self.dragViewHeight/2;
        CGFloat y = self.frame.size.height/2 - h/2;
        CGRectMake(x, y, w, h);
    });
    [self adjustCacheView];
    [self adjustPlayTimeView];
    [self adjustDragView];
}

#pragma mark - Private

- (void)adjustCacheView
{
    self.cacheView.frame = ({
        CGRect frame = self.railView.bounds;
        frame.size.width = self.cacheProgress*frame.size.width;
        frame;
    });
}

- (void)adjustPlayTimeView
{
    self.playTimeView.frame = ({
        CGRect frame = self.railView.bounds;
        frame.size.width = self.playProgress*frame.size.width;
        frame;
    });
}

- (void)adjustDragView
{
    self.dragView.frame = ({
        CGFloat w = self.frame.size.height;
        CGFloat h = w;
        CGFloat y = 0;
        CGFloat x = self.playTimeView.frame.size.width - self.dragViewHeight/2;
        CGRectMake(x, y, w, h);
    });
    self.dragImgView.frame = ({
        CGFloat w = self.dragViewHeight;
        CGFloat h = w;
        CGFloat x = self.dragView.frame.size.width/2 - w/2;
        CGFloat y = x;
        CGRectMake(x, y, w, h);
    });
}

#pragma mark - Event

- (void)dragEvent: (UIPanGestureRecognizer *)pan
{
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:
        {
            self.beganPoint = self.dragView.frame.origin;
            self.dragging = YES;
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            CGPoint point = [pan translationInView:self];
            CGFloat min = 0;
            CGFloat max = 0;
            min = self.dragViewHeight/2 - self.frame.size.height/2;
            max = self.frame.size.width - self.frame.size.height/2 - self.dragViewHeight/2;
            self.dragView.frame = ({
                CGRect frame = self.dragView.frame;
                CGFloat x = self.beganPoint.x + point.x;
                x = MAX(x, min);
                x = MIN(x, max);
                frame.origin.x = x;
                frame;
            });
            self.playTimeView.frame = ({
                CGRect frame = self.playTimeView.frame;
                CGPoint point = [self.dragView.superview convertPoint:self.dragView.center toView:self.railView];
                frame.size.width = point.x;
                frame;
            });
            self.playProgress = self.playTimeView.frame.size.width/self.railView.frame.size.width;
            if (self.dragChangedBlock)
            {
                self.dragChangedBlock(self.playProgress);
            }
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            self.dragging = NO;
            if (self.dragEndBlock)
            {
                self.dragEndBlock(self.playProgress);
            }
        }
            break;
        default:
            break;
    }
}

- (void)tapRailEvent: (UITapGestureRecognizer *)ges
{
    CGPoint location = [ges locationInView:self.railView];
    CGFloat progress = location.x/self.railView.frame.size.width;
    progress = MIN(1, progress);
    progress = MAX(0, progress);
    if(self.railTapBlock)
    {
        self.railTapBlock(progress);
    }
}

#pragma mark - Setter

- (void)setPlayProgress:(CGFloat)playProgress
{
    _playProgress = playProgress;
    if (!self.dragging)
    {        
        [self adjustPlayTimeView];
        [self adjustDragView];
    }
    if (self.playProgressChangedBlock)
    {
        self.playProgressChangedBlock(playProgress);
    }
}

- (void)setCacheProgress:(CGFloat)cacheProgress
{
    _cacheProgress = cacheProgress;
    [self adjustCacheView];
}

#pragma mark - Getter

- (UIView *)railView
{
    if (!_railView)
    {
        self.railView = ({
            UIView *view = [UIView new];
            view.backgroundColor = [UIColor whiteColor];
            view.clipsToBounds = YES;
            [self addSubview:view];
            view;
        });
    }
    return _railView;
}

- (UIView *)cacheView
{
    if (!_cacheView)
    {
        self.cacheView = ({
            UIView *view = [UIView new];
            view.backgroundColor = [UIColor lightGrayColor];
            view.clipsToBounds = YES;
            [self.railView addSubview:view];
            view;
        });
    }
    return _cacheView;
}

- (UIView *)playTimeView
{
    if (!_playTimeView)
    {
        self.playTimeView = ({
            UIView *view = [UIView new];
            view.backgroundColor = [UIColor colorWithRed:0.7 green:0.25 blue:0.25 alpha:1];
            view.clipsToBounds = YES;
            [self.railView addSubview:view];
            view;
        });
    }
    return _playTimeView;
}

- (UIView *)dragView
{
    if (!_dragView)
    {
        self.dragView = ({
            UIView *view = [UIView new];
            [view setResponseArea:UIEdgeInsetsMake(7, 7, 7, 7)];
            [view addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragEvent:)]];
            [self addSubview:view];
            view;
        });
    }
    return _dragView;
}

- (UIImageView *)dragImgView
{
    if (!_dragImgView)
    {
        self.dragImgView = ({
            UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"RBMediaPlayer.bundle/slider"]];
            [self.dragView addSubview:view];
            view;
        });
    }
    return _dragImgView;
}

@end
